const pool = require('../tools/getPool');
const sendResponse = require('../tools/sendResponse');

module.exports = function(req, res){

	let id = req.body.id;
	let name = req.body.name;
	let headimg = req.body.headimg;

	let params = [];
	let sql = "UPDATE user SET name = ? WHERE id = ?;";
	params.push(name);

	if (headimg != undefined && headimg != null) {
		sql = "UPDATE user SET name = ?, headimg = ? WHERE id = ?";
		params.push(headimg);
	}

	params.push(id);

	// 获取数据库连接
	pool.getConnection(function(err, connection) {
		if (err) {
			console.log(err);
			sendResponse('error','数据库内部错误', null, res);
			return;
		}
		// 执行语句
		connection.query(sql, params, function (error, rows, fields) {
			// 释放连接
			connection.release();

			if (error) { 
				console.log(error);
				sendResponse('error','数据库内部错误', null, res);
				return;
			}

			req.session.name = name;
			if (headimg != undefined && headimg != null) {
				req.session.headimg = headimg;
			}
			sendResponse('ok','操作成功', rows[0], res);

		})	

	});

}